//
//  SHMyAcountViewController.m
//  youjian03
//
//  Created by 常琼 on 14/10/9.
//  Copyright (c) 2014年 com.shouhuobao. All rights reserved.
//

#import "SHMyAcountViewController.h"
#import "SHAssetsUserListCell.h"
#import "SHMyAccountModel.h"
#import "SHHowToUseViewController.h"

@interface SHMyAcountViewController ()

@property(nonatomic,copy) NSString *token;

@property (weak, nonatomic) IBOutlet UIWebView *webView;

@property (weak, nonatomic) IBOutlet UILabel *redmoney;//数字

@property(nonatomic,weak) UIView *blackCover;//第一层黑色蒙版
@property(nonatomic,weak) UIView *mainalert;//正经的说明视图
@property(nonatomic,weak) UIView *finalview;//透明的蒙版


@property(nonatomic,copy) NSString *amount;

@property(nonatomic,strong) NSMutableArray *array;

@end

@implementation SHMyAcountViewController
-(NSArray *)array
{
    if (_array == nil)
    {
        _array = [NSMutableArray array];
    }
    return  _array;
}
/**
 *  点击使用说明
 */
- (IBAction)howToUse:(UIButton *)sender {
    
    //添加黑色蒙版
    UIView *view2  = [[UIView alloc] initWithFrame:[UIScreen mainScreen].bounds];
    UIImageView *imageView =  [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"mask_bg_view"]];
    [view2 addSubview:imageView];
    
    
    
    
    [[UIApplication sharedApplication].keyWindow addSubview:view2];
    self.blackCover = view2;
    
    
    //添加xib view
    SHHowToUseViewController *howtouerVc = [[SHHowToUseViewController alloc] init];
    howtouerVc.view .frame = CGRectMake((HMScreenW-260)/2  ,(HMScreenH-400)/2,260, 400);
    howtouerVc.view.layer.cornerRadius = 6;
    [self.blackCover addSubview:howtouerVc.view];
    
    
    
    
    
    
    
    //添加透明蒙版
    UIView *finalview = [[UIView alloc ] initWithFrame:[UIScreen mainScreen].bounds];
    // [finalview setAlpha: 0];
    self.finalview = finalview;
    [[UIApplication sharedApplication].keyWindow addSubview:self.finalview];
    
    
    
    
    //在透明蒙版上新建手势
    UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] init];
    
    [self.finalview addGestureRecognizer:tap];
    [tap addTarget:self action:@selector(tapView)];
    
}
/**
 *  点击蒙版
 */
-(void)tapView
{
    [self.blackCover removeFromSuperview];
    [self.mainalert removeFromSuperview ];
    [self.finalview removeFromSuperview];
    
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    
    
    //标题
    
    //添加一个有件快递按钮
    UIButton *youjian =  [[UIButton alloc] initWithFrame:CGRectMake(0, 0, 69, 17)];
    [youjian setTitle:@"我的账户" forState:UIControlStateNormal];
    [youjian setTitleColor:[UIColor redColor] forState: UIControlStateNormal];
    
    [youjian setFont:[UIFont fontWithName:@"Helvetica-Bold" size:20]];
    
    youjian.userInteractionEnabled = NO;
    
    self.navigationItem.titleView = youjian;
    
    
    //添加左侧按钮
    self.navigationItem.leftBarButtonItem = [UIBarButtonItem itemWithImageName:@"back" highImageName:@"back" target:self action:@selector(leftButton)];
    
    //得到数据库
    NSString *doc = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) lastObject];
    NSString *filename = [doc stringByAppendingPathComponent:@"youjian.sqlite"];
    
    // 2.得到数据库
    FMDatabase *db = [FMDatabase databaseWithPath:filename];
    if([db open])
    {
        FMResultSet *resultset =  [db executeQuery:@"select * from userinfo"];
        while ([resultset next]) {
            NSString *token = [resultset stringForColumn:@"token"];
            
            
            self.token = token;
        }
        
    }
    
    //我的账户_view
    NSDictionary *dic  = @{@"token":self.token};
    [SHHTTPTOOL PostWithDic:dic andAction:@"userRedbagDetail" success:^(id responseObj) {
        
        
        NSArray *a =((NSDictionary *)responseObj[@"msg"])[@"list"];
        NSString *type =   ( (NSDictionary *)a[0])[@"type"];
        NSString *time =( (NSDictionary *)a[0])[@"time"];
        NSString *expirationTime =( (NSDictionary *)a[0])[@"expirationTime"];
        self.amount = ( (NSDictionary *)a[0])[@"amount"];
        
        self.redmoney .text =[NSString stringWithFormat:@"%@",self.amount];
        
        if (a.count>=1)
        {
            for(int i = 1;i< a.count;i++)
            {
                SHMyAccountModel *model = [[SHMyAccountModel alloc] init];
                model.type =   ( (NSDictionary *)a[i])[@"type"];
                model .time = ( (NSDictionary *)a[i])[@"time"];
                model.amount =  ( (NSDictionary *)a[i])[@"amount"];
                [self.array addObject:model];
                
                
            }
        }
        
        [self.assetsUseList reloadData];
        
        
        
    } failure:^(NSError *error) {
        
    }];
    
    UIView *view = [[NSBundle mainBundle] loadNibNamed:@"MyAccount" owner:self options:nil][0];
    view.frame = CGRectMake(0, 64, HMScreenW, HMScreenH-64);
    [self.view addSubview: view];
    [self.redmoney setTextColor:[UIColor blackColor]];
    
    
    self.assetsUseList.delegate = self;
    self.assetsUseList.dataSource = self;
    self.assetsUseList.separatorStyle = UITableViewCellSeparatorStyleNone;
    
    
}

//-(void)viewWillAppear:(BOOL)animated
//{
//    [super viewWillAppear:animated];
//    NSLog(@"");
//
//}

/**
 *  点击左侧按钮
 */
-(void)leftButton
{
    [UIView animateWithDuration:0.5 animations:^{
        self.navigationController .view.transform = CGAffineTransformTranslate(self.navigationController.view.transform, 260, 0);
    }completion:^(BOOL finished) {
        
        //发送通知
        NSDictionary *dic = @{@"key":@"3"};
        [[NSNotificationCenter defaultCenter] postNotificationName:@"dianji" object:nil userInfo:dic];
        
        self.navigationController .view.transform = CGAffineTransformIdentity;
        
    }];
    
}

#pragma -mark assetsUserList tableView



-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return self.array.count;
}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return 50.0f;
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    static NSString * cellName = @"cell";
    SHAssetsUserListCell * cell = [tableView dequeueReusableCellWithIdentifier:cellName];
    if (cell == nil) {
        cell = [[SHAssetsUserListCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellName];
        
        
    }
    //SHMyAccountModel *mm = self.array[indexPath.row];
    [cell assetsUserListCell:self.array IndexPaht:indexPath];
    return cell;
}

@end
